import re
from urllib import parse
from Education.model.session import session
from Education.util.analyzer.getNetPostKey import get_viewstate_eventvalidation


def check_login(user, pwd):
    url = "http://124.160.107.91:9090/default2.aspx"

    dict01 = {
        'TextBox1': user,
        'TextBox2': pwd,
        'RadioButtonList1': '%D1%A7%C9%FA',
        'Button1': '',
    }

    # 获取viewstate和eventvalidation值
    response = session.get(url=url)

    dict01.update(get_viewstate_eventvalidation(response))

    headers = {
        'Content-Type': "application/x-www-form-urlencoded",
    }
    # 进行url编码
    data = parse.urlencode(dict01)
    # 登录
    response = session.post(url=url, data=data, headers=headers)
    html = response.text
    raw_name = re.search('<span id="xhxm">(.+?)</span>', html)
    if not raw_name:
        return None
    else:
        name = raw_name.group(1)[:-2]
    return name
